﻿using System;
using System.Configuration;
using System.ServiceModel;
using Catalyst.ServiceModel;

namespace Catalyst.ServiceModel
{
	public abstract class CloudProxy<T, I> : Catalyst.ServiceModelZero.BaseProxy<T, I>
		where T : CloudProxy<T, I>, new()
		where I : class
	{
		static T _instance;
		static System.ServiceModel.Channels.Binding _binding;
		static EndpointAddress _address;

		protected static I CreateChannel(System.ServiceModel.Channels.Binding binding = null, EndpointAddress endpointAddress = null, Uri scope = null)
		{
			if (_instance == null)
			{
				_instance = new T();
			}

			if (binding != null)
			{
				_binding = binding;
			}
			else
			{
				_binding = _instance.GetBinding();
			}

			if (endpointAddress != null)
			{
				_address = endpointAddress;
			}
			else
			{
				_address = _instance.GetEndpointAddress();
			}

			return CloudChannelFactory<I>.CreateChannel(_binding, _address, scope);
		}
	}
}
